Skip to content

Fix HAProxy peer entries to include name, address, and port#462

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-haproxy-peer-configuration
Draft

Fix HAProxy peer entries to include name, address, and port#462
Copilot wants to merge 4 commits intomainfrom
copilot/fix-haproxy-peer-configuration

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

HAProxy was discarding the entire peers section because entries were rendered as peer <address> instead of the required peer <name> <address>:<port> format.

Changes

  • state/peers.py (new): Add PeersInformation mixin class with HAPROXY_PEER_PORT = 10000 constant and formatted_peer_entries / peer_tcp_port @property methods that pre-render peer lines (e.g. 10-68-79-144 10.68.79.144:10000).
  • State dataclasses: IngressRequirersInformation, IngressPerUnitRequirersInformation, and HaproxyRouteRequirersInformation now subclass PeersInformation, exposing the peer formatting as properties.
  • src/haproxy.py: reconcile_ingress and reconcile_haproxy_route now read formatted_peer_entries and peer_tcp_port directly from the state objects instead of calling a standalone helper.
  • Templates (haproxy_route.cfg.j2, haproxy_ingress.cfg.j2, haproxy_ingress_per_unit.cfg.j2): Add bind *:{{ peer_tcp_port }} and consume pre-rendered peer entries.
  • Tests: Add unit tests for the PeersInformation mixin covering IPv4, IPv6, empty input, and port property.

Before/After

# Before (invalid — HAProxy discards this)
peers haproxy_peers
    peer 10.68.79.144

# After
peers haproxy_peers
    bind *:10000
    peer 10-68-79-144 10.68.79.144:10000

Copilot AI and others added 2 commits April 13, 2026 11:46
Add HAPROXY_PEER_PORT constant (10000) and _format_peer_entries helper
to render peer entries as '<name> <address>:<port>' instead of just
'<address>'. Also add 'bind *:<port>' to the peers section in all
templates. This fixes HAProxy discarding peers due to invalid config.

Agent-Logs-Url: https://github.com/canonical/haproxy-operator/sessions/71b67230-85a7-4437-ba07-80b49a93c036

Co-authored-by: Thanhphan1147 <42444001+Thanhphan1147@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix invalid peer configuration in HAProxy Fix HAProxy peer entries to include name, address, and port Apr 13, 2026
Copilot AI requested a review from Thanhphan1147 April 13, 2026 11:50
Comment thread haproxy-operator/src/haproxy.py Outdated
Comment on lines +290 to +309
def _format_peer_entries(peers: list[IPvAnyAddress]) -> list[str]:
"""Format peer IP addresses into HAProxy peer entry strings.

Each entry is formatted as ``<name> <address>:<port>`` where ``<name>``
is derived from the IP address with non-alphanumeric characters replaced
by hyphens.

Args:
peers: List of peer IP addresses.

Returns:
list[str]: Formatted peer entry strings.
"""
entries: list[str] = []
for addr in peers:
name = str(addr).replace(".", "-").replace(":", "-")
entries.append(f"{name} {addr}:{HAPROXY_PEER_PORT}")
return entries


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This should be a @Property of the charmstate dataclass ( ingress_requirers_information and haproxy_route_requirers_information ). In fact we can make a separate dataclass for this and subclass it in each of the mentioned charmstate dataclass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved HAPROXY_PEER_PORT and the formatting logic into a PeersInformation mixin class in state/peers.py with formatted_peer_entries and peer_tcp_port as @property methods. IngressRequirersInformation, IngressPerUnitRequirersInformation, and HaproxyRouteRequirersInformation all subclass it. src/haproxy.py now reads the properties from the state objects. See d1ac83e.

…ataclasses

Move HAPROXY_PEER_PORT and peer formatting logic from src/haproxy.py
into a PeersInformation mixin class in state/peers.py. All three state
dataclasses (IngressRequirersInformation, IngressPerUnitRequirersInformation,
HaproxyRouteRequirersInformation) now subclass PeersInformation and expose
formatted_peer_entries and peer_tcp_port as @Property methods.

Agent-Logs-Url: https://github.com/canonical/haproxy-operator/sessions/29068634-9a26-492a-a765-4ae185a92429

Co-authored-by: Thanhphan1147 <42444001+Thanhphan1147@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Peers discarded in HAProxy due to invalid configuration

2 participants